package cn.gz.demo.ui;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.Timer;
import java.util.TimerTask;

import cn.gz.demo.R;
import cn.gz.demo.databinding.ViewPagerBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.DisplayUtil;
import cn.gz.demo.utils.LogUtil;
import cn.gz.demo.view.RotationPageTransformer;
import cn.gz.demo.view.ViewPagerScroller;

/**
 * Created by Liuyz on 2017/12/7.
 */
public class ViewPagerActivity extends BaseActivity {
    private ViewPagerBinding binding;
    private int[] imageResID = new int[]{R.mipmap.welcome_1, R.mipmap.welcome_2, R.mipmap.welcome_3, R.mipmap.welcome_1, R.mipmap.welcome_2, R.mipmap.welcome_3};  // 图片

    private ImageAdapter imageadapter1, imageadapter2, imageadapter3;
    private int index = 0;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_viewpager);

        initPager1();
        initPager2();
        initPager3();
    }

    private void initPager3() {
        imageadapter3 = new ImageAdapter();
        binding.idViewpager3.setPageMargin(30);
        binding.idViewpager3.setOffscreenPageLimit(3);

        int pagerWidth = screenWidth - DisplayUtil.dip2px(context, 50);
        ViewGroup.LayoutParams lp = binding.idViewpager3.getLayoutParams();
        if (lp == null) {
            lp = new ViewGroup.LayoutParams(pagerWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        } else {
            lp.width = pagerWidth;
        }
        binding.idViewpager3.setLayoutParams(lp);
        binding.idViewpager3.setPageMargin(DisplayUtil.dip2px(context, 20));
        binding.idViewpager3.setAdapter(imageadapter3);
    }

    private void initPager2() {
        imageadapter2 = new ImageAdapter();
        binding.idViewpager2.setPageMargin(30);
        binding.idViewpager2.setOffscreenPageLimit(3);

        int pagerWidth = screenWidth - DisplayUtil.dip2px(context, 36);
        ViewGroup.LayoutParams lp = binding.idViewpager2.getLayoutParams();
        if (lp == null) {
            lp = new ViewGroup.LayoutParams(pagerWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        } else {
            lp.width = pagerWidth;
        }
        binding.idViewpager2.setLayoutParams(lp);
        binding.idViewpager2.setPageMargin(DisplayUtil.dip2px(context, 10));
        binding.idViewpager2.setAdapter(imageadapter2);
    }

    private void initPager1() {
        imageadapter1 = new ImageAdapter();
        binding.idViewpager.setPageMargin(30);
        binding.idViewpager.setOffscreenPageLimit(3);
        binding.idViewpager.setPageTransformer(false, new RotationPageTransformer());

        ViewPagerScroller pagerScroller = new ViewPagerScroller(this);
        pagerScroller.setScrollDuration(1000);//设置时间，时间越长，速度越慢
        pagerScroller.initViewPagerScroll(binding.idViewpager);
        binding.idViewpager.setAdapter(imageadapter1);

        startTime();

        binding.idViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position;
                LogUtil.showLog("......position......." + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.idViewpager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: //按下
                        if (timer != null) {
                            timer.cancel();
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:  //移动
                        if (timer != null) {
                            timer.cancel();
                        }
                        break;
                    case MotionEvent.ACTION_UP: //抬起
                        if (timer != null) {
                            timer.cancel();
                        }
                        startTime();
                        break;

                }
                return false;
            }
        });
    }

    private void startTime() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // 需要做的事:发送消息
                if (imageResID.length > 0) {
                    ++index;
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                }
            }
        }, 6000, 6000); // 1s后执行task,经过1s再次执行
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                binding.idViewpager.setCurrentItem(index);
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    private class ImageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        // 指定本次页面( arg1 )将要显示的内容与当前页面( arg0 )的内容是否一致
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0.equals(arg1);
        }

        // 添加页面
        @Override
        public Object instantiateItem(ViewGroup viewpager, int position) {
            View view = LayoutInflater.from(context).inflate(R.layout.viewpager_item, viewpager, false);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv);
            imageView.setImageResource(imageResID[position % imageResID.length]);
            viewpager.addView(view);
            return view;
        }

        // 销毁页面
        @Override
        public void destroyItem(ViewGroup viewpager, int position, Object object) {
            ((ViewPager) viewpager).removeView((View) object);
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

    }
}
